/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pgcolor.cc
  \date			Jun 2012
  \author		TNick

  \brief		Contains the implementation of PgColor class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<QColor>

#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/layer/pglayglob.h>
#include	<cpg/logic/layer/pglayspc.h>
#include	<cpg/logic/pgctx.h>
#include	<cpg/logic/pgspace.h>

#include	"pgcolor.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PgColor::PgColor	( CpgDoc * doc )
	: PgCharacteristic( doc )
{
	color_ = 0;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgColor::~PgColor	( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgColor *			PgColor::clone				( CpgDoc * destin, int & idx )
{
	Q_ASSERT( destin != NULL );
	PgColor * ret = new PgColor( destin );

	ret->color_ = color_;
	ret->setName( name() );

	destin->addColor( ret );
	idx = destin->colorsCount() - 1;

	return ret;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgColor::nameForId			( PgColor::Id id )
{
	switch		( id )	{

	case	BLACK:
		return QObject::tr( "black" );
	case	WHITE:
		return QObject::tr( "white" );
	case	DARKGRAY:
		return QObject::tr( "dark gray" );
	case	GRAY:
		return QObject::tr( "gray" );
	case	LIGHTGRAY:
		return QObject::tr( "light gray" );
	case	RED:
		return QObject::tr( "red" );
	case	GREEN:
		return QObject::tr( "green" );
	case	BLUE:
		return QObject::tr( "blue" );
	case	CYAN:
		return QObject::tr( "cyan" );
	case	MAGENTA:
		return QObject::tr( "magenta" );
	case	YELLOW:
		return QObject::tr( "yellow" );
	case	DARKRED:
		return QObject::tr( "dark red" );
	case	DARKGREEN:
		return QObject::tr( "dark green" );
	case	DARKBLUE:
		return QObject::tr( "dark blue" );
	case	DARKCYAN:
		return QObject::tr( "dark cyan" );
	case	DARKMAGENTA:
		return QObject::tr( "dark magenta" );
	case	DARKYELLOW:
		return QObject::tr( "dark yellow" );
	case	BYLAYER:
		return QObject::tr( "by layer" );
	case	BYBLOCK:
		return QObject::tr( "by block" );
	case	FOREGROUND:
		return QObject::tr( "foreground" );
	case	BACKGROUND:
		return QObject::tr( "background" );
	default:
		return QString();
	}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgColor::nameAndId		(
		QStringList & name_list, QList<int> & id_list )
{

#	define	nameAndId_helper(i)	\
	id_list.append( i ); name_list.append( nameForId( i ) );

	nameAndId_helper( BYLAYER );
	nameAndId_helper( BYBLOCK );
	nameAndId_helper( FOREGROUND );
	nameAndId_helper( BACKGROUND );
	nameAndId_helper( BLACK );
	nameAndId_helper( WHITE );
	nameAndId_helper( DARKGRAY );
	nameAndId_helper( GRAY );
	nameAndId_helper( LIGHTGRAY );
	nameAndId_helper( RED );
	nameAndId_helper( GREEN );
	nameAndId_helper( BLUE );
	nameAndId_helper( CYAN );
	nameAndId_helper( MAGENTA );
	nameAndId_helper( YELLOW );
	nameAndId_helper( DARKRED );
	nameAndId_helper( DARKGREEN );
	nameAndId_helper( DARKBLUE );
	nameAndId_helper( DARKCYAN );
	nameAndId_helper( DARKMAGENTA );
	nameAndId_helper( DARKYELLOW );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QStringList			PgColor::names			(
		CpgDoc * doc, const PgColor::List & list )
{
	QStringList		sl;
	foreach( int id, list )
	{
		if ( id < USER_BASE )
			sl.append( nameForId( (PgColor::Id)id ) );
		else
			sl.append( doc->color( id )->name() );
	}
	return sl;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgColor::isIdValid		( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->colorsCount() )
			return true;
		return false;
	}
	else
	{
		switch		( id )	{
		case	BLACK:
		case	WHITE:
		case	DARKGRAY:
		case	GRAY:
		case	LIGHTGRAY:
		case	RED:
		case	GREEN:
		case	BLUE:
		case	CYAN:
		case	MAGENTA:
		case	YELLOW:
		case	DARKRED:
		case	DARKGREEN:
		case	DARKBLUE:
		case	DARKCYAN:
		case	DARKMAGENTA:
		case	DARKYELLOW:
		case	BYLAYER:
		case	BYBLOCK:
		case	FOREGROUND:
		case	BACKGROUND:
			return true;
		default:
			return false;
		}
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QColor				PgColor::qtColor		( PgCtx & ctx, int id )
{

	if ( id >= 0 )
	{
		if ( id < ctx.document_->colorsCount() )
			return QColor( ctx.document_->color( id )->rgb() );
		Q_ASSERT( false );
		return QColor();
	}
	else
	{
		for ( ;; )
		{
			switch		( id )	{
			case	BLACK:
			case	WHITE:
			case	DARKGRAY:
			case	GRAY:
			case	LIGHTGRAY:
			case	RED:
			case	GREEN:
			case	BLUE:
			case	CYAN:
			case	MAGENTA:
			case	YELLOW:
			case	DARKRED:
			case	DARKGREEN:
			case	DARKBLUE:
			case	DARKCYAN:
			case	DARKMAGENTA:
			case	DARKYELLOW:
				return QColor( colorFromPgToQt( id ) );
			case	FOREGROUND:
				if ( ctx.space_ != NULL )
				{
					return ctx.space_->qtSpecialForeGround( ctx );
				}
				return QColor( Qt::white );

			case	BACKGROUND:
				if ( ctx.space_ != NULL )
				{
					return ctx.space_->qtSpecialBackGround( ctx );
				}
				return QColor( Qt::black );

			case	BYLAYER:
				if ( ctx.layer_ != NULL )
				{
					id = ctx.layer_->color();
					if ( id == BYLAYER )
					{
						Q_ASSERT( false );
						id = FOREGROUND;
						continue;
					}
					continue;
				}

				/* a layer should have been present */
				Q_ASSERT( false );
				id = FOREGROUND;
				continue;

			case	BYBLOCK:
				if ( ctx.block_def_ != NULL )
				{
					/** @todo block  return ctx.block_def_->getColor();
					*	This may be part of another block, so use its own method
					*/
				}

				/* a block should have been present */
				Q_ASSERT( false );
				id = FOREGROUND;
				continue;

			default:
				Q_ASSERT( false );
				id = FOREGROUND;
				continue;
			}
		}
	}
	return QColor( Qt::lightGray );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgColor::asString		( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->colorsCount() )
			return doc->color( id )->name();
		return QString();
	}
	else
	{
		return nameForId( (PgColor::Id)id );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgColor::asXML			(
		PgCtx & ctx, int id, BBlocks::ByteBuffer * dest)
{
/*
	QDomDocument x_doc = dest.ownerDocument();
	QDomComment cmnt = x_doc.createComment(
				"pg-color="+asString( ctx.document_, id ) );
	dest.appendChild( cmnt );
	dest.setAttribute( "pg-color", id );
	QColor	col = qtColor( ctx, id );
	dest.setAttribute( "pg-color-explicit", col.rgba() );
*/
	/** @todo asXML */
	Q_UNUSED( ctx );
	Q_UNUSED( id );
	Q_UNUSED( dest );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int					PgColor::copy			(
		CpgDoc * source, CpgDoc * destin, int value )
{
	Q_ASSERT( source != NULL );
	Q_ASSERT( destin != NULL );
	Q_ASSERT( isIdValid( source, value ) );

	/* can we directly use this value? */
	if ( source == destin )
		return value;

	if ( value >= 0 )
	{
		int i;
		/*PgColor new_col = */ source->color( value )->clone( destin, i );
		return i;
	}
	else
	{
		return value;
	}
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
